/*	kd:
	
	NOTE TO SELF:
	must be built when the shop list is set.
	
	I dunno how exactly yet, but the player's ptrs to instances of these must
	change when the shop list changes, so in essence, to allow the new item
	glow, you have to manage creating this table, too. The way the tables are
	stored doesn't matter, as long as there's a player key to them.
	
	I put everything into the Gc_PlayerShopData table, which is to be built when
	the player is created and filled in with data from a static eventhandler (it
	exists). All that needs to be done: Pass ptr to item handlers.
	
	*/

// Store at load, ensure ptrs valid; specialised for shop.
class Gc_PlayerShopData {
	// Data pointers built elsewhere:
	private Gcs_GlowingItems	glowing_items;
	private bool				is_init;
	
	void Init (Gcs_GlowingItems glow) {
		glowing_items	= glow;
		is_init			= true;
	}
	
	Gcs_GlowingItems GlowingItemsPtr () const {
		return glowing_items;
	}
}

class Gcs_GlowingItems {
	private Array <Gcs_GlowItemTable>	shop_glow;
	private int size;
	
	// Needs the shop list so we know how many shops there are and their indices...
	void Init (Gcs_ShopList shops) {
		ClearTables();
		size = shops.ShopCount();
		
		for(int i = size; 0 <= --i;) {
			Gcs_GlowItemTable table = new("Gcs_GlowItemTable");
			shop_glow.push(table);
		}
		
		size = shop_glow.size();
	}
	
	void Reset () {
		ClearTables();
	}
	
	void EmptyTables () {
		for(int i = 0; i < size; i++) {
			shop_glow [i].clear();
		}
	}
	
	int TableCount () const {
		return size;
	}
	
	Gcs_GlowItemTable TableForShopIndex (uint shop_i) const {
		return shop_i < size ? shop_glow [shop_i] : NULL;
	}
	
	// I find it odd that I need to basically write these functions over and
	// over. Is there a way to avoid this?
	void Add (uint shop_index, uint item_index) {
		if(size <= shop_index) {
			Console.printf("\c[ice]Gcs_GlowingItems::Add(%i, %i): "
				"\c-shop_index out of bounds. Failed shop list setup?",
				shop_index, item_index);
			return;
		}
		
		Gcs_GlowItemTable table = shop_glow [shop_index];
		
		if(table) {
			table.AddGlowingItem(item_index);
		}
	}
	
	void Remove (int shop_index, int item_index) {
		Gcs_GlowItemTable table = shop_glow [shop_index];
		
		if(table) {
			table.RemoveGlowingItem(item_index);
		}
	}
	
	private void ClearTables () {
		for(int i = size; 0 <= --i;) {
			shop_glow [i].Destroy();
			shop_glow.delete(i);
		}
		
		shop_glow.clear();
		size = 0;
	}
}

class Gcs_GlowItemTable {
	// Existence of an item in this array implies it will glow in the shop it
	// belongs to.
	
	// Note: Can't make this private because zs is a retard about dealing with
	// arrays elsewhere.
	/* private */ Array <int>		items;
	private int				size;
	
	int ItemCount () const {
		return size;
	}
	
	int At (int i) const {
		return items [i];
	}
	
	void AddGlowingItem (int item_index) {
		if(size == 0 || size == items.find(item_index)) {
			items.push(max(0, item_index));
			size++;
		}
	}
	
	void RemoveGlowingItem (int item_index) {
		if(size == 0) {
			return;
		}
		
		int i = items.find(item_index);
		
		if(i != size) {
			items.delete(i);
			size--;
		}
	}
	
	void Clear () {
		size = 0;
		items.clear();
	}
}